package cn.school.sys.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.sys.dao.SysTenantInfoDao;
import cn.school.sys.dao.SysUserDao;
import cn.school.sys.entity.SysTenantInfo;
import cn.school.sys.entity.SysUser;
import cn.school.sys.service.SysTenantInfoService;
import cn.school.sys.vo.SysTenantInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: lx
 * @date: 2023/4/20 16:32
 * @description:
 */
@Service
public class SysTenantInfoServiceImpl extends ServiceImpl<SysTenantInfoDao, SysTenantInfo> implements SysTenantInfoService {

    @Autowired
    private SysTenantInfoDao sysTenantInfoDao;

    @Resource
    private SysUserDao sysUserDao;

    @Override
    public int create(SysTenantInfo sysTenantInfo) {
        LambdaQueryWrapper<SysTenantInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysTenantInfo::getTenantNo,sysTenantInfo.getTenantNo());
        SysTenantInfo isRepeat = sysTenantInfoDao.selectOne(wrapper);
        if(isRepeat != null){
            return -1;
        }else {
            sysTenantInfo.setCreateTime(new Date());
            return this.baseMapper.insert(sysTenantInfo);
        }
    }

    @Override
    public IPage<SysTenantInfo> findPageList(SysTenantInfoVo vo) {
        int count = sysTenantInfoDao.findPageCount(vo);
        int startNum = (vo.getPageNum() - 1) * vo.getPageSize();
        List<SysTenantInfo> list = sysTenantInfoDao.findPageList(startNum,vo.getPageSize(),vo);
        Page<SysTenantInfo> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public int update(SysTenantInfo sysTenantInfo) {
        sysTenantInfo.setModifyTime(new Date());
        return this.baseMapper.updateById(sysTenantInfo);
    }

    @Override
    public SysTenantInfo getTenantInfoById(Long id) {
        LambdaQueryWrapper<SysTenantInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysTenantInfo::getId, id);
        return sysTenantInfoDao.selectOne(queryWrapper);
    }

    @Override
    public int updateSysTenantId(SysTenantInfo sysTenantInfo) {
        Long userId = SysUserUtils.getSysUser().getId();
        SysUser sysUser = sysUserDao.selectById(userId);
        sysUser.setTenantId(String.valueOf(sysTenantInfo.getId()));
        return sysUserDao.updateById(sysUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        LambdaQueryWrapper<SysTenantInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysTenantInfo::getId, id);
        int delete = this.baseMapper.delete(queryWrapper);
        if(delete > 1){
            //删除租户后将对应的租户下的用户状态更改为无效
            sysUserDao.updateStatusBatchByTenantId(id,"0");
        }
        return delete;
    }
}
